CherryPy always returning HTTP 200 [closed]

Posted by DarkArctic on Stack Overflow See other posts from Stack Overflow or by DarkArctic
Published on 2012-06-08T15:20:42Z Indexed on 2012/06/16 9:16 UTC
Read the original article Hit count: 212

Filed under:
|

I'm having a bit of a problem when browsing to a non-existent resource. I get a response code of 200 instead of 404.

I'm using the MethodDispatcher and I have a class that overloads the __getattr__ method to instantiate a resource if a child exists or to return AttributeError if one doesn't. My class is always returning the AttributeError correctly, but the data I actually get is always from the last good resource.

Here's a simplified (except for __getattr__) version of my class:

class BaseResource(object):

    exposed = True

    def __init__(self, name):
        self.children = []  # Pretend this has child resources

    def __getattr__(self, name):
        if name in self._children:
            uuid, application, obj_type, server = self._children[name]
            try:
                resource = getattr(app[application], obj_type)
            except AttributeError as e:
                raise cherrypy.HTTPError(500, e)
            return resource(uuid)
        else:
           raise AttributeError('Child with name \'{}\' could not be found.'.format(name))

    def GET(self):
        cherrypy.log.error('*** {} not found, raising AttributeError'.format(name))
        return 'GET request for {}'.format(self._name)

So fetching I get the following when I browse to the following resources:

http://localhost:8000/users - This resource exists, so it returns it correctly.

http://localhost:8000/users/fake - This returns the "users" resource giving an HTTP 200.

http://localhost:8000/users/fake/reallyfake - This returns the "users" resource again.

So my question is, where can I start looking to find out why my code isn't returning a 404 for a non-existent resource. I'm sure I've done something wrong, but I'm not sure what.


Whatever I did wrong I've undone and I'm now getting a 404 returned correctly. I'm sorry I can't give any detail on what the issue was, but I'm honestly not sure what I did.

© Stack Overflow or respective owner

Related posts about python

Related posts about cherrypy